package com.sibco.client.organization;

import java.util.Vector;
import com.bstek.dorado.utils.variant.VariantSet;
import com.bstek.dorado.common.DoradoContext;
import com.bstek.dorado.data.AbstractDatasetListener;
import com.bstek.dorado.data.Dataset;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.data.db.SqlDataset;
import com.sibco.client.system.User;
import com.sibco.client.common.Tools;

public class OrgPubListener extends AbstractDatasetListener{
	public boolean beforeLoadData(Dataset dataset) throws Exception {
	    SqlDataset ds=(SqlDataset)dataset;      
	    String sql="";
	    DoradoContext context = DoradoContext.getContext();
	    User user=(User)context.getAttribute(context.SESSION,"User");
	    String lg=Tools.toStr(user.getCountry());
	    if (ds.getId().equals("datasetOrg")){	    	
	    	sql="SELECT CLT_RT_COMPANY_ORG.ORG_ID,CLT_RT_COMPANY_ORG.COMPANY_ID,CLT_RT_COMPANY_ORG.ORG_CODE,"
	    	   +"CLT_RT_COMPANY_ORG.ORG_CN_NAME,CLT_RT_COMPANY_ORG.ORG_EN_NAME,";
//	    	if ("cn".equals(lg)){
//	    		sql+="CLT_RT_COMPANY_ORG.ORG_CN_NAME ,";
//	    	}
//	    	if ("en".equals(lg)){
//	    		sql+="CLT_RT_COMPANY_ORG.ORG_EN_NAME ORG_NAME,";
//	    	}
	    	sql+="CLT_RT_COMPANY_ORG.CONTACT_MAN,CLT_RT_COMPANY_ORG.CONTACT_TEL1,CLT_RT_COMPANY_ORG.CONTACT_TEL2,"
	    	   +"CLT_RT_COMPANY_ORG.EMAIL,CLT_RT_COMPANY_ORG.FAX,CLT_RT_COMPANY_ORG.CONTACT_ADDRESS,CLT_RT_COMPANY_ORG.CONTACT_ZIP,CLT_RT_COMPANY_ORG.SUPER_ORG_ID,"
	    	   +"CLT_RT_COMPANY_ORG.DEPT_LEVEL,CLT_RT_COMPANY_ORG.MANAGER_ID,CLT_RT_COMPANY_ORG.IS_VALID,CLT_RT_COMPANY_ORG.INPUT_MAN,CLT_RT_COMPANY_ORG.INPUT_DATE,"
	    	   +"CLT_RT_COMPANY_ORG.MODIFY_MAN,CLT_RT_COMPANY_ORG.MODIFY_DATE "
	    	   +"FROM CLT_RT_COMPANY_ORG,CLT_RT_USER "
	    	   +"WHERE CLT_RT_COMPANY_ORG.IS_VALID = '1' AND CLT_RT_COMPANY_ORG.MANAGER_ID=CLT_RT_USER.USER_ID(+) "
	    	   +"AND (CLT_RT_COMPANY_ORG.SUPER_ORG_ID IS NULL OR CLT_RT_COMPANY_ORG.SUPER_ORG_ID=0) ";
	    	if (!Tools.toStr(user.getCompanyId()).equals("")){
	    	   sql+="AND CLT_RT_COMPANY_ORG.COMPANY_ID='"+Tools.toStr(user.getCompanyId())+"'";
	    	}	    	   
	    }
	    
	    if (ds.getId().equals("datasetEmpRole")){	
	    	String empId=Tools.toStr(ds.parameters().getString("EMPLOYEE_ID")); 
	    	String comId=Tools.toStr(ds.parameters().getString("COMPANY_ID")); 
	    	sql="select r.ROLE_ID,r.ROLE_CODE,r.ROLE_CN_NAME,r.ROLE_EN_NAME,r.COMPANY_ID "
               +"from clt_rt_role r "
               +"where (r.company_id is null or r.company_id=0 or r.company_id="+comId+") "
               +"and r.is_valid=1 and r.role_code<>'199' "
               +"order by r.role_code ";	    	    	   
	    }
	    
	    ds.setSql(sql);
	    return true;
	  }

	  public void afterLoadData(Dataset dataset) throws Exception {
		  String sql="";
		  DoradoContext context = DoradoContext.getContext();
		  if (dataset.getId().equals("datasetEmpRole")){	
			  DBStatement state = new DBStatement();
			  String empId=Tools.toStr(dataset.parameters().getString("EMPLOYEE_ID")); 
		      String comId=Tools.toStr(dataset.parameters().getString("COMPANY_ID")); 
		      sql="select role_id from clt_rt_emp_role er where er.employee_id="+empId+" and er.company_id="+comId+" "
		         +"and er.is_valid=1";
		      state.setSql(sql);
		      java.util.List list=state.queryForList();
		      state.close();
		      String roleStr=",";
		      for(int i=0;i<list.size();i++){
		    	  VariantSet var=(VariantSet)list.get(i);
		    	  roleStr+=var.getString(0)+",";
		      }
		      
			  dataset.moveFirst();
			  while(!dataset.isLast()){
				  String roleId=Tools.toStr(dataset.getString("ROLE_ID"));
				  if (roleStr.indexOf(","+roleId+",")>=0){
					  dataset.setValue("select", true);
				  }else{
					  dataset.setValue("select", false);
				  }
				  dataset.moveNext();
			  }
		  }
	  }
}
